﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Young3.FMSearch.Business.Entities;

namespace Young3.FMSearch.Business.Managers
{
    public class GameManager
    {
        private bool fmLoaded;

        public GameManager()
        {
            fmLoaded = false;
        }

        public string GetCurrentFMVersion()
        {
            return ProcessManager.FmVersion;
        }

        public bool FMLoaded()
        {
            return fmLoaded;
        }

        public bool CheckProcessAndGame()
        {
            FMProcess fmProcess = new FMProcess();
            Process[] fmProcesses = Process.GetProcessesByName("fm");
            Process[] wsmProcesses = Process.GetProcessesByName("wsm");
            int cnt = 0;
            if (fmProcesses.Length > 0 || wsmProcesses.Length > 0)
            {
                Process activeProcess;
                if (fmProcesses.Length > 0)
                    activeProcess = fmProcesses[0];
                else
                    activeProcess = wsmProcesses[0];

                fmProcess.Pointer = ProcessMemoryApi.OpenProcess(0x38, 1, (uint)activeProcess.Id);
                fmProcess.EndPoint = ProcessManager.GetProcessEndPoint(fmProcess.Pointer);
                fmProcess.Process = activeProcess;

                ProcessManager.fmProcess = fmProcess;

                ProcessManager.FmVersion = fmProcess.Process.MainModule.FileVersionInfo.FileVersion + ".0";
                cnt = ProcessManager.ReadInt32(0x205371c + ProcessManager.getVersionOffset);

                if (cnt > 0)
                    fmLoaded = true;
                else
                    fmLoaded = false;
            }
            return fmLoaded;
        }
    }
}
